package com.fire.rabbitmq.queue;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ：Mr.Fire
 * @date ：Created in 2021/4/25 17:06
 * @description：路由模式
 * @modified By：
 * @version: $
 */
@Configuration
public class QueueRouter {

    public static final String DIRECT_EXCHANGE = "directExchange";
    public static final String QUEUE_DIRECT_A = "direct.A";

    public static final String QUEUE_DIRECT_B = "direct.B";

    /**
     * 创建一个direct交换机
     * @return
     */
    @Bean
    DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    Queue queueDirectNameA() {
        return new Queue(QUEUE_DIRECT_A);
    }


    /**
     * 创建队列
     * @return
     */
    @Bean
    Queue queueDirectNameB() {
        return new Queue(QUEUE_DIRECT_B);
    }


    /**
     * 将direct.A队列绑定到directExchange交换机中，使用a.key作为路由规则
     * @param queueDirectNameA
     * @param directExchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessageDirectA(Queue queueDirectNameA, DirectExchange directExchange) {
        return BindingBuilder.bind(queueDirectNameA).to(directExchange).with("a.key");
    }

    /**
     * 将direct.B队列绑定到directExchange交换机中，使用nr.b.key作为路由规则
     * @param queueDirectNameB
     * @param directExchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessageDirectB(Queue queueDirectNameB, DirectExchange directExchange) {
        return BindingBuilder.bind(queueDirectNameB).to(directExchange).with("b.key");
    }
}
